# -*- encoding=utf-8 -*-
import sys
import pandas as pd

if __name__=="__main__":
    df = pd.read_csv(sys.argv[1])
    first_colomn_name = '{} binary rpm package'.format(sys.argv[2])

    df.drop(labels=0,inplace=True)
    df.fillna('',inplace=True)

    #新增两列包名
    package_name_list = []
    #print(list(df[first_colomn_name]))
    for source_package in list(df[first_colomn_name]):
        if source_package!='':
            package_name_list.append(source_package.rsplit(".", 2)[0].rsplit("-", 2)[0])
        else:
            package_name_list.append('')
    df['first_package_name_list']=package_name_list


    #软件包交集数量
    common_rpm_list = df[(df['compare result'] == '1') | (df['compare result'] == '1.1') | (df['compare result'] == '2') | (df['compare result'] == '3')]
    #centos软件包数量(去除4选项，即前者有，后者无)
    centos_rpm_list = df[(df['compare result'] == '1') | (df['compare result'] == '1.1') | (df['compare result'] == '2') | (df['compare result'] == '3')
                          | (df['compare result'] == '5')]
    #软件包总数
    total_rmp_list = df[(df['compare result'] == '1') | (df['compare result'] == '1.1') | (df['compare result'] == '2') | (df['compare result'] == '3')
                         | (df['compare result'] == '4') | (df['compare result'] == '5')]


    #part 2
    result_dict={}
    all_src_name_key = list(common_rpm_list[first_colomn_name])
    soft_package_table = df[(df['compare result'] != '4') | (df['compare result'] != '5')]

    temp_simple_reslut = [
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0]
    ]

    for package in all_src_name_key:
        tmp_table = soft_package_table[soft_package_table[first_colomn_name] == package]
        tmp_require = tmp_table[tmp_table["compare type"] == "rpm requires"]
        tmp_provide = tmp_table[tmp_table["compare type"] == "rpm provides"]
        has_require = True
        if (len(list(tmp_require["compare type"])) == 0 or len(list(tmp_provide["compare type"])) == 0):
            has_require = False
        tmp_list = list(tmp_table['compare result'])
        if '1' in tmp_list and len(tmp_list)-1 != 0:
            result_dict[package]=1
            temp_simple_reslut[0][0] += 1
            temp_simple_reslut[0][1] += round(tmp_list.count('same')/(len(tmp_list)-1),2)
            temp_simple_reslut[0][2] += list(tmp_require["compare result"]).count("same")
            temp_simple_reslut[0][3] += list(tmp_provide["compare result"]).count("same")
            temp_simple_reslut[0][4] += 1 if has_require else 0
        elif '1.1' in tmp_list and len(tmp_list)-1 != 0:
            result_dict[package]=0.7+round(tmp_list.count('same')/(len(tmp_list)-1),2)*0.3
            temp_simple_reslut[1][0] += 1
            temp_simple_reslut[1][1] += round(tmp_list.count('same')/(len(tmp_list)-1),2)
            temp_simple_reslut[1][2] += list(tmp_require["compare result"]).count("same")
            temp_simple_reslut[1][3] += list(tmp_provide["compare result"]).count("same")
            temp_simple_reslut[1][4] += 1 if has_require else 0
        elif '2' in tmp_list and len(tmp_list)-1 != 0:
            result_dict[package] = 0.3 + round(tmp_list.count('same') / (len(tmp_list) - 1), 2) * 0.7
            temp_simple_reslut[2][0] += 1
            temp_simple_reslut[2][1] += round(tmp_list.count('same')/(len(tmp_list)-1),2)
            temp_simple_reslut[2][2] += list(tmp_require["compare result"]).count("same")
            temp_simple_reslut[2][3] += list(tmp_provide["compare result"]).count("same")
            temp_simple_reslut[2][4] += 1 if has_require else 0
        elif '3' in tmp_list and len(tmp_list)-1 != 0:
            result_dict[package]= round(tmp_list.count('same')/(len(tmp_list)-1),2)
            temp_simple_reslut[3][0] += 1
            temp_simple_reslut[3][1] += round(tmp_list.count('same')/(len(tmp_list)-1),2)
            temp_simple_reslut[3][2] += list(tmp_require["compare result"]).count("same")
            temp_simple_reslut[3][3] += list(tmp_provide["compare result"]).count("same")
            temp_simple_reslut[3][4] += 1 if has_require else 0
        else:
            pass
    import operator

    print('软件包交集数量：{} 并集数量：{}'.format(len(common_rpm_list), len(total_rmp_list)))
    print("过程数据: {}".format(temp_simple_reslut))
    print('软件范围相似度:',round(len(common_rpm_list) / len(total_rmp_list), 3) * 10,'%')
    # print('软件范围相似度(situation1:分母为oecp比对项第二个全量软件包):',round(len(common_rpm_list)/len(centos_rpm_list),2)*0.1)
    print('软件内容相似度:',round(sum(result_dict.values())/len(result_dict), 3) * 80,'%')
    # print('similar_result_one--->',similar_result_one)